package by.bsu.scs.presentation.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import by.bsu.scs.db.dao.TaskAttachmentHome;
import by.bsu.scs.model.TaskAttachment;
import by.bsu.scs.model.User;
import by.bsu.scs.presentation.form.TaskForm;
import by.bsu.scs.util.manager.SessionManager;

/**
 * @author Alexey Hursevich
 * @version 1.0
 * 
 * All actions connected with study tasks.
 */
public class InstructorTaskAction extends DispatchAction {
	
	private static final String UNCHECKED_STATE = "unchecked";

	private static final String ADD = "add";
	private static final String LIST = "list";
	private static final String UNCHECKED = "unchecked";
	private static final String MARK = "mark";
	private static final String COUNT_UNCHECKED_AJAX = "countUncheckedAjax";

	/**
	 * Adds new task - not implemented yet!
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping forward path
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TaskForm taskForm = (TaskForm) form;

		return mapping.findForward(ADD);
	}
	
	/**
	 * Lists tasks - not implemented yet!
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping forward path
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TaskForm taskForm = (TaskForm) form;

		return mapping.findForward(LIST);
	}
	
	/**
	 * Lists study tasks sent by students and not checked
	 * by instructor.
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping forward path
	 */
	public ActionForward unchecked(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TaskForm taskForm = (TaskForm) form;

		TaskAttachmentHome dao = new TaskAttachmentHome();
		User user = SessionManager.getInstance().getUser(request);
		ArrayList <TaskAttachment> uncheckedTaskList = (ArrayList<TaskAttachment>) dao.getTaskAttachmentByInstructorAndState(user, UNCHECKED_STATE);
		
		taskForm.setUncheckedTaskList(uncheckedTaskList);
		
		return mapping.findForward(UNCHECKED);
	}
	
	/**
	 * Forwards to task evaluation page - not implemented yet!
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping forward path
	 */
	public ActionForward mark(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TaskForm taskForm = (TaskForm) form;

		return mapping.findForward(MARK);
	}
	
	public ActionForward countUncheckedTasks(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TaskForm taskForm = (TaskForm) form;

		TaskAttachmentHome dao = new TaskAttachmentHome();
		User user = SessionManager.getInstance().getUser(request);
		ArrayList <TaskAttachment> uncheckedTaskList = (ArrayList<TaskAttachment>) dao.getTaskAttachmentByInstructorAndState(user, UNCHECKED_STATE);
		
		if(uncheckedTaskList != null) {
			taskForm.setCountUncheckedTasks( uncheckedTaskList.size() );
		}
		else {
			taskForm.setCountUncheckedTasks(0);
		}
		
		return mapping.findForward(COUNT_UNCHECKED_AJAX);
	}
}
